package com.ap.core.jbehave.submitrtp;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.hamcrest.CoreMatchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import com.ap.core.cxf.jaxb.common.ResponseWithIntentType;
import com.ap.core.cxf.jaxb.managertp.SubmitRTP1Request;
import com.ap.core.cxf.jaxb.managertp.SubmitRTP1Response;
import com.ap.core.cxf.jaxb.managertp.SubmitRTPMessageType;
import com.ap.core.jbehave.BaseTransitionRTPSteps;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.core.util.Nullable;
import com.ap.core.util.TypedKey;
import com.core.ap.wsdl.managertpservice_1.ManageRTP1PortType;


/**
 * This class contains the steps used by the test harness JBehave instances.
 */
public class SubmitRTPSteps extends BaseTransitionRTPSteps {

    protected static final TypedKey<Nullable<SubmitRTP1Response>> SubmitRTP1Response = new TypedKey<>("ResponseRTPType");

    private SubmitRTP1Request submitRTPRequestMOB;
    private SubmitRTP1Request submitRTPRequestBRN;

    public SubmitRTPSteps(StoryContext storyContext) {
        super(storyContext);
    }


    @Given("the OSB has submitted a structurally valid SUBMITRTPFORMOB request to APCore")
    public void givenSUBMITRTPFORMOBSubmitted() {


        submitRTPRequestMOB = createSubmitRTPRequest(SubmitRTPMessageType.SUBMITRTPFORMOB);
        submitRTPRequestMOB.getRTPSenderInfo().getDistributorInfo().setDistributorId("000001");
        submitRTPRequestMOB.getRTPSenderInfo().getDetailMerchantInfo().getMerchantInfo().setMerchantId("000001");

        LOG.info("When -  a Consumer chooses to make payment using AP service through m-com site to purchase a digital product");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Request.class, submitRTPRequestMOB);
        System.out.println("Starting requesting time from client : " + System.currentTimeMillis());
    }

    @When("APCore receives the structurally valid SUBMITRTPFORMOB")
    public void whenAPCoreReceviedSUBMITRTPFORMOB() throws Exception {

        final ManageRTP1PortType singnedPort = factory.getBean(ManageRTP1PortType.class);
        SubmitRTP1Response responseRTPType = singnedPort.submitRTP1(submitRTPRequestMOB);
        System.out.println("Response Received from server: " + System.currentTimeMillis());
        LOG.info("When -  Response Received");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Response.class, responseRTPType);

        storyContext.put(SubmitRTP1Response, Nullable.nullable(responseRTPType));
        
    }

    @Then("the DistributorID within the request should be validated against the DistributorID reference data")
    public void thenDistributorIDValidated() {

    }

    @Then("the MerchantID within the request should be validated against the MerchantID reference data")
    public void thenMerchantIDValidated() {
    }

    @Then("all RTP information is stored in the database with timestamp")
    public void thenRTPInformationStored() {
    }

    @Then("a unique 18 character APTransactionID should be generated")
    public void thenAPTransactionIDGenerated() {
        Nullable<SubmitRTP1Response> responseRTPType = storyContext.get(SubmitRTP1Response);
        assertThat("Didn't receive ResponseWithIntentType", false, CoreMatchers.is(equalTo(responseRTPType.hasNull())));

        final ResponseWithIntentType responseWithIntentType = responseRTPType.value().getResponseInfo();

        assertThat("APTransactionId is not as expected", false, CoreMatchers.is(equalTo(responseWithIntentType.getApTransactionId() == null)));
    }

    @Then("a ReconciliationID should be generated")
    public void thenReconciliationIDGenerated() {
        Nullable<SubmitRTP1Response> responseRTPType = storyContext.get(SubmitRTP1Response);
        assertThat("Didn't receive ResponseWithIntentType", false, CoreMatchers.is(equalTo(responseRTPType.hasNull())));

        final ResponseWithIntentType responseWithIntentType = responseRTPType.value().getResponseInfo();
        assertThat("ReconId is not as expected", false, CoreMatchers.is(equalTo(responseWithIntentType.getReconId() == null)));
    }

    @Then("the status of the new T_ORDER_PAYMENT record should be RTPSUBMITTED")
    public void thenStoredAsRTPSUBMITTED() {
    }

    @Then("a SUCCESSFUL RESPONSETORTP should be generated and sent to OSB")
    public void thenRESPONSETORTPSentToOSB() {
        Nullable<SubmitRTP1Response> responseRTPType = storyContext.get(SubmitRTP1Response);
        doSubmitRTPResponseAssertions(responseRTPType, submitRTPRequestMOB);

        final ResponseWithIntentType responseWithIntentType = responseRTPType.value().getResponseInfo();
        assertThat("MobAppIntent is not as expected", false, CoreMatchers.is(equalTo(responseWithIntentType.getMobAppIntent() == null)));
    }


    @Given("the OSB has submitted a structurally valid SUBMITRTPFORBRN request to APCore")
    public void ecomgivenSUBMITRTPFORMOBSubmitted() {
    }

    @When("APCore receives the structurally valid SUBMITRTPFORBRN")
    public void whenecomAPCoreReceviedSUBMITRTPFORMOB() throws Exception {

        final ManageRTP1PortType singnedPort = factory.getBean(ManageRTP1PortType.class);
        submitRTPRequestBRN = createSubmitRTPRequest(SubmitRTPMessageType.SUBMITRTPFORBRN);
        submitRTPRequestBRN.getRTPSenderInfo().getDistributorInfo().setDistributorId("000001");
        submitRTPRequestBRN.getRTPSenderInfo().getDetailMerchantInfo().getMerchantInfo().setMerchantId("000001");

        LOG.info("When -  a Consumer chooses to make payment using AP service through m-com site to purchase a digital product");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Request.class, submitRTPRequestBRN);
        SubmitRTP1Response responseRTPType = singnedPort.submitRTP1(submitRTPRequestBRN);

        LOG.info("When -  Response Received");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Response.class, responseRTPType);

        storyContext.put(SubmitRTP1Response, Nullable.nullable(responseRTPType));
    }


    @Then("a unique 6 charecter BRN should be generated")
    public void thenEcomAPTransactionIDGenerated() {
        Nullable<SubmitRTP1Response> responseRTPType = storyContext.get(SubmitRTP1Response);
        assertThat("Didn't receive ResponseWithIntentType", false, CoreMatchers.is(equalTo(responseRTPType.hasNull())));

        final ResponseWithIntentType responseWithIntentType = responseRTPType.value().getResponseInfo();

        assertThat("BRN is not as expected", false, CoreMatchers.is(equalTo(responseWithIntentType.getBRN() == null)));
    }


    @Then("a SUCCESSFUL RESPONSETORTP including BRN should be generated and sent to OSB")
    public void thenEcomRESPONSETORTPSentToOSB() {
        Nullable<SubmitRTP1Response> responseRTPType = storyContext.get(SubmitRTP1Response);
        assertThat("Didn't receive ResponseWithIntentType", false, CoreMatchers.is(equalTo(responseRTPType.hasNull())));

        final ResponseWithIntentType responseWithIntentType = responseRTPType.value().getResponseInfo();
        doSubmitRTPResponseAssertions(responseRTPType, submitRTPRequestBRN);
        assertThat("MobAppIntent is not as expected", true, CoreMatchers.is(equalTo(responseWithIntentType.getMobAppIntent() == null)));
        assertThat("BRN is not as expected", false, CoreMatchers.is(equalTo(responseWithIntentType.getBRN() == null)));

        

    }

}



